/*
 *     Copyright 2000-2011 Francois de Bertrand de Beuvron
 * 
 *     This file is part of CoursBeuvron.
 * 
 *     CoursBeuvron is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     CoursBeuvron is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with CoursBeuvron.  If not, see <http://www.gnu.org/licenses/>.
 */
package fr.insa.beuvron.tps.m2.dessin2DV3;

import java.util.Formatter;

/**
 * repris de fr.insa.beuvron.utils.StringUtil pour que ce package soit indépendant
 * @author francois
 */
public class Utils {
     /**
   * the concatenation of s with itself nbg times.
   * @param s
   * @param nbr
   * @return the concatenation of s with itself nbg times.
   */
  public static String mult(String s,int nbr) {
    StringBuffer res = new StringBuffer();
    for (int i = 0 ; i < nbr ; i ++) {
      res.append(s);
    }
    return res.toString();
  }

  /**
   * put indenter at the begining of each line in s
   * @param s
   * @param indenter
   * @return indented s
   */
  public static String specialIndent(String s, String indenter) {
    StringBuffer res = new StringBuffer();
    boolean beginLine = true;
    for (int i = 0 ; i < s.length() ; i ++) {
      if (beginLine) {
        res.append(indenter);
        beginLine = false;
      }
      char cur = s.charAt(i);
      res.append(cur);
      if (cur == '\n') {
        beginLine = true;
      }
    }
    return res.toString();
  }

  /**
   * add spaces before each line in s
   * @param s the original string
   * @param nbrSpace the number of space to insert at the begining of each line in s
   * @return indented s
   */
  public static String indent(String s,int nbrSpace) {
    String spaces = mult(" ",nbrSpace);
    return specialIndent(s,spaces);
  }
  
  public static String formatDouble(double d,int tailleTot) {
      StringBuilder buf = new StringBuilder();
      Formatter f = new Formatter(buf);
      f.format("%" + tailleTot + "g", d);
      return buf.toString();
  }
 
}
